<!--
  @Descripttion:：功能描述
  @Author：yejianbo
  @Date：2022年05月26日 16:05:45
-->
<template>
  <div class="swiper banner" id="banner">
    <div class="swiper-wrapper">
      <div
        class="swiper-slide"
        v-for="(item, index) in bannerList"
        @click="toLink(item.forwardUrl, item.type == 5)"
        :key="index"
        :style="item.type == 5 ? 'cursor:pointer;' : ''"
      >
        <div class="banner-img" v-if="item.type == 3 || item.type == 5">
          <img :src="item.imageUrl" v-imgError alt="" />
        </div>
        <div v-if="item.type == 4" class="banner-video">
          <div class="video-box">
            <video :src="item.imageUrl" class="video-el" muted autoplay loop="true"></video>
          </div>
        </div>
        <!-- <div v-if="item.type == 4" class="banner-video">
          <vueVideoPlayer
            :src="item.imageUrl"
            :key="index"
            :ref="'video' + index"
            :loop="true"
          ></vueVideoPlayer>
        </div> -->
      </div>
    </div>
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
    <div class="paginationRow">
      <div
        class="paginationItem"
        v-for="(item, index) in bannerList"
        :key="index"
        :class="[bannerIndex == index ? 'active' : '']"
        @click="changeIndex(index)"
      ></div>
    </div>
  </div>
</template>

<script>
import Swiper from "swiper";
import "swiper/css/swiper.min.css";
import { getBanner } from "../../../request/api";
import vueVideoPlayer from "@/components/videoPlayer/videoPlayer.vue";
export default {
  // 组件名称
  name: "demo",
  // 组件参数 接收来自父组件的数据
  props: {},
  // 局部注册的组件
  components: {
    vueVideoPlayer,
  },
  // 组件状态值
  data() {
    return {
      swiper: null,
      bannerList: [],
      bannerIndex: 0,
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    initSwiper() {
      let that = this;
      this.swiper = new Swiper("#banner", {
        autoHeight: false,
        spaceBetween: 20,
        autoplay: false,
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
        // loop: true,
        on: {
          slideChangeTransitionStart: function () {
            that.bannerIndex = this.activeIndex;
          },
          slideChangeTransitionEnd: function () {
            that.bannerList.map((item, index) => {
              if (item.type == 4) {
                let refStr = `video${index}`;
                if (Number(that.bannerIndex) === Number(index)) {
                  that.$refs[refStr][0] && that.$refs[refStr][0].playVideo();
                } else {
                  that.$refs[refStr][0] && that.$refs[refStr][0].pause();
                }
              }
            });
          },
        },
      });
    },
    init() {
      getBanner({ pageNo: 1, pageSize: 10 }).then((res) => {
        if (res.code === 0 && res.data) {
          let list = res.data.list.filter(
            (v) =>
              v.status === "0" && (v.type == 3 || v.type == 4 || v.type == 5)
          );
          this.bannerList = list;
          this.$nextTick(function () {
            this.initSwiper();
          });
        }
      });
    },
    changeIndex(index) {
      if (this.bannerIndex == index) return;
      this.bannerIndex = index;
      this.swiper.slideTo(index, 1000, false);
    },
    toLink(url, type) {
      if (!type || !url) return;
      window.open(url, '_blank')
    },
  },
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
   */
  created() {},
  /**
   * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
   * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
   */
  mounted() {},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.banner {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.banner-img {
  display: block;
  width: 100%;
  height: 100%;
  position: relative;
  // padding-top: 38%;
  img {
    position: absolute;
    display: block;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover !important;
  }
}
.video-box {
    position: relative;
    width: 100%;
    height: 100%;
    .video-el {
        position:absolute;
        top:0;
        left:0;
        right:0;
        bottom:0;
        width: 100%;
        object-fit: cover;
        background-color: #121212;
    }
}
.paginationRow {
  width: 100%;
  height: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  bottom: 30px;
  left: 0;
  z-index: 2;
  .paginationItem {
    width: 16px;
    height: 16px;
    box-sizing: border-box;
    border-radius: 50%;
    background: #fff;
    margin: 0 15px;
    cursor: pointer;
    &.active {
      background: #3d6cb0;
    }
  }
}
.banner-video {
  display: block;
  width: 100%;
  height: 100%;
  video {
    height: 100%;
  }
}
.swiper {
  width: 100%;
  height: auto;
}
.swiper-slide {
  text-align: center;
  font-size: 18px;
  background: #fff;
  img {
    // max-height: calc(100vh - 20px);
    height: 100%;
    object-fit: contain;
  }
}
.swiper-button-next,
.swiper-button-prev {
  width: 70px;
  height: 70px;
  top: 50%;
  transform: translateY(-50%);
  &::after {
    content: none;
  }
}
.swiper-button-next.swiper-button-disabled,
.swiper-button-prev.swiper-button-disabled {
  opacity: 0 !important;
}
.swiper-button-next {
  right: 11%;
  background: url("../../../assets/images/home/button_next.png") no-repeat
    center / cover;
}
.swiper-button-prev {
  left: 11%;
  background: url("../../../assets/images/home/button_prev.png") no-repeat
    center / cover;
}
</style>
